import { api, TWsClient } from '@/assets/api';

class TLedTpl {
    constructor(_core, _tplCode) {
        this.core = _core;
        this.tplCode = _tplCode;
        this.msg = {
            time: 0,
            data: {}
        };
        this.handlers = {
            onRunning: null
        };
    }

    onSetData() {
        var me = this;
        try {

        } catch (error) {
            console.log(error)
        }
    }
}
class TLedTpl_Welcome extends TLedTpl {
    constructor(_core, _tplCode) {
        super(_core, _tplCode);
    }

    get matchName() {

    }
}
class TLedTpl_Schedule extends TLedTpl {
    constructor(_core, _tplCode) {
        super(_core, _tplCode);
    }
}
class TLedTpl_Real extends TLedTpl {
    constructor(_core, _tplCode) {
        super(_core, _tplCode);
    }
}
class TLedTpl_Irm extends TLedTpl {
    constructor(_core, _tplCode) {
        super(_core, _tplCode);
    }
}
class TLed {
    constructor() {
        this.runningTime = (new Date()).getTime();
        this.api = api;
        this.settings = {
            localKey: "equts_led_localKey_",
            tpls: [
                { code: "welcome", tplClass: TLedTpl_Welcome },
                { code: "schedule", tplClass: TLedTpl_Schedule },
                { code: "real", tplClass: TLedTpl_Real },
                { code: "irm", tplClass: TLedTpl_Irm },
            ]
        };
        this.ledDatas = {
            currentTplCode: "",
            tplDatas: {}
        };
        this.tpls = {};
        this.commonDatas = {
            eventName: "",
            bgImg: null
        };
    }
    get currentTpl() {
        var me = this;
        res = null;
        try {
            var tpl = me.tpls[me.ledDatas.currentTplCode];
            if (tpl && tpl != null) {
                res = tpl;
            }
        } catch (error) {
            console.log(error)
        }
    }
    loadTplDatas() {
        var me = this;
        try {
            me.settings.forEach(se => {
                var tpl = new se.tplClass(this, se.code);
                me.tpls[tpl.code] = tpl;
                var localKey = me.settings.localKey + tpl.code;
                try {
                    var content = window.localStorage.getItem(localKey);
                    var msg = JSON.parse(content);
                    tpl.msg = msg;
                } catch (er) {
                    console.log(er)
                }
            })
        } catch (error) {
            console.log(error)
        }
    }
    setTplData(tplCode, tplData) {
        var me = this;
        try {
            me.ledDatas.tplDatas[tplCode] = tplData;
            me.ledDatas.currentTplCode = tplCode;
            me.tpls[tplCode].msg = { time: (new Date()).getTime(), data: tplData };
            var localKey = me.settings.localKey + tplCode;
            window.localStorage.setItem(localKey, JSON.stringify(me.tpls[tplCode].msg));
            me.tpls[tplCode].onSetData();
        } catch (error) {
            console.log(error)
        }
    }
    setTplRunningTime(rt) {
        var me = this;
        try {
            me.runningTime = rt;
            if (me.currentTpl != null && me.currentTpl.handlers.onRunning != null) {
                me.currentTpl.handlers.onRunning(rt);
            }
        } catch (error) {
            console.log(error)
        }
    }
}